package cn.edu.scau.cmi.linyiqu.client;

import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

import cn.edu.scau.cmi.linyiqu.composite.safe.SafeComponent;
import cn.edu.scau.cmi.linyiqu.composite.safe.SafePerson;
import cn.edu.scau.cmi.linyiqu.composite.safe.SafeTeam;

public class SafeCompositeClient {
	private static Scanner scanner = new Scanner(System.in);
	private static Set<SafeTeam> teamSet = new HashSet<SafeTeam>();// 存储所有团队的信息。
	private static SafeTeam firstTeam = null;

	public static void main(String[] args) {
		while (true) {
			System.out.println("1.创建第一个团队\n2.修改团队名称\n3.为某团队添加下属团队\n4.为某团队添加下属个人\n5.为某团队删除下属团队或个人\n"+"请输入数字：");
			String string = scanner.nextLine();
			int input=0;
			try {
				input = Integer.parseInt(string);
			}
			catch(Exception e) {
				//不处理
			}
			switch (input) {
			case 1:
				if (firstTeam != null) {
					System.out.println("已经存在初始团队");
					break;
				}
				createTeam();
				break;
			case 2:
				modifyTeam();
				break;
			case 3:
				addTeam();
				break;
			case 4:
				addPerson();
				break;
			case 5:
				removeComponent();
				break;
			default:
				break;
			}
			if (firstTeam != null)
				System.out.println(firstTeam.show(0));// 输出全体信息。
		}
	}

	private static void createTeam() {
		System.out.println("请输入团队名称：");
		String name = scanner.nextLine();
		SafeTeam team = new SafeTeam(name);
		teamSet.add(team);
		if (firstTeam == null) {
			firstTeam = team;
		}
		teamSet.add(team);
	}

	private static int modifyTeam() {
		System.out.println("请输入要修改的团队旧名：");
		String name = scanner.nextLine();
		SafeTeam team = null;
		for (SafeTeam tmp : teamSet) {
			if (tmp.getName().equals(name)) {
				team = tmp;
				if (team != null && !teamSet.isEmpty()) {
					System.out.println("请输入要修改的团队新名：");
					name = scanner.nextLine();
					team.setName(name);
				}
				return 1;
			}
		}
		System.out.println("找不到该团队");
		return 0;
	}

	private static int addTeam() {
		System.out.println("请输入上级团队名称：\n");
		String name = scanner.nextLine();
		SafeTeam team = null;
		for (SafeTeam tmp : teamSet) {
			if (tmp.getName().equals(name)) {
				team = tmp;
				if (team != null && !teamSet.isEmpty()) {
					System.out.println("请输入下属团队名称：");
					name = scanner.nextLine();
					SafeTeam newTeam = new SafeTeam(name);
					team.addChild((SafeComponent) newTeam);
					teamSet.add(newTeam);
				}
				return 1;
			}
		}
		System.out.println("找不到该团队");
		return 0;
	}

	private static int addPerson() {
		System.out.println("请输入上级团队名称：\n");
		String name = scanner.nextLine();
		SafeTeam team = null;
		for (SafeTeam tmp : teamSet) {
			if (tmp.getName().equals(name)) {
				team = tmp;
				if (team != null && !teamSet.isEmpty()) {
					System.out.println("请输入下属个人名称：");
					name = scanner.nextLine();
					SafePerson newTeam = new SafePerson(name);
					team.addChild((SafeComponent) newTeam);
				}
				return 1;
			}
		}
		System.out.println("找不到该团队");
		return 0;
	}

	private static int removeComponent() {
		System.out.println("请输入上级团队名称：");
		String name = scanner.nextLine();
		SafeTeam team = null;
		for (SafeTeam tmp : teamSet) {
			if (tmp.getName().equals(name)) {
				team = tmp;
				if (team != null && !teamSet.isEmpty()) {
					System.out.println("请输入下属团队或成员名称：");
					name = scanner.nextLine();
					for (SafeComponent tmpComponent : team.getChildrenSet()) {
						if (tmpComponent.getName().equals(name)) {
							team.removeChild(tmpComponent);
							if (tmpComponent instanceof SafeTeam)
								teamSet.remove(tmpComponent);
						}
					}
				}
				return 1;
			}
		}
		System.out.println("找不到该团队");
		return 0;
	}

}